// pages/Coterie/detail.js
import md5 from '../../utils/md5.js';
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		qian_on:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		this.suo = false;
		that.setData({
			options_id: options.id
		})
		//文章
		app.request(app.apiDomain + "index.php?c=group&a=article", {
			id:options.id
		}, function(res) {
			that.setData({
				Coterie_detail: res.data.data
			})
		});
		//评论
		app.request(app.apiDomain + "index.php?c=group&a=getCommentList", {
			page: 1,
			size: 20,
			id: options.id
		}, function(res) {
			// console.log(res)
			that.setData({
				comment: res.data.data
			})
		});
		
		
		var num=0;
		const vm = this;
		var off=true;
		//
		app.request(app.apiDomain + "index.php?c=group&a=dslist",{
			article_id:options.id,
			size:20,
		}, function(res) {
			// console.log(res)
			that.setData({
				com: res.data.data
			})
			// console.log(vm.data.com.length)
			if(vm.data.com){
				if(vm.data.com.length>1){
					setInterval(function(){
						if(off){
							num++
							vm.setData({
									top_num:'-'+ 8 * num +'vw'
							})
							if(num==(vm.data.com.length-1)){
								off=false;
							}
						}else{
							num--
							vm.setData({
									top_num:'-'+  8 * num +'vw'
							})
							if(num==0){
								off=true;
							}
						}
					},3000);
				}
			}
		});
		
		
	},
	zan_click:function (e) {
		var that = this;
		that.setData({
			zan_fl:'on'
		})	
		// console.log(e.currentTarget.dataset.id);
		app.request(app.apiDomain + "index.php?c=group&a=zan", {
			article_id:e.currentTarget.dataset.id
		}, function(res) {
			
				var a ={}
				a.icon = wx.getStorageSync('icon')
				// console.log(a)
				if(that.data.Coterie_detail.zan_list.length<6){
					that.data.Coterie_detail.zan_list.unshift(a)
				}else{
					that.data.Coterie_detail.zan_list.unshift(a)
					that.data.Coterie_detail.zan_list.splice(6,1,'')
				}
				that.setData({
					['Coterie_detail.zan_list']:that.data.Coterie_detail.zan_list,
					['Coterie_detail.zan']:parseFloat(that.data.Coterie_detail.zan) + 1
				})
				// console.log(that.data.Coterie_detail.zan_list);
// 			}else{
// 				that.setData({
// 					zan_fl:'on'
// 				})
// 			}
			
		});
		
	},
	qian_fl_click:function(){
		this.setData({
			qian_on:'on',
		})
	},
	qian_off:function(){
		this.setData({
			qian_on:'',
		})
	},
	qian_click:function(e){
		var a = e.currentTarget.dataset.id;
		this.qian_num = a >=0 ? a:20;
		if(a == 'a'){
			this.qiam_type = '2'
		}else if(a == 'b'){
			this.qiam_type = '3'
		}else if(a == 'c'){
			this.qiam_type = '4'
		}else{
			this.qiam_type = '1'
		}
 		// console.log(a,this.qiam_type)
		this.setData({
			qian_type:'type_' + e.currentTarget.dataset.id
		})
	},
	fan_da_click:function(e){
		// console.log()
		wx.previewImage({
			current:e.currentTarget.dataset.src,
			urls:this.data.Coterie_detail.imgs
		})
// 		this.setData({
// 			fan_da_on:'on',
// 			image_src:e.currentTarget.dataset.src
// 		});
	},
// 	fan_da_off:function(){
// 		this.setData({
// 			fan_da_on:''
// 		});
// 	},
	qian_submit:function(e){
		var that = this;
		if(this.data.qian_type){
			
			const innerAudioContext = wx.createInnerAudioContext('qian_1')
			innerAudioContext.autoplay = true
			innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/img/zou_lu/qian.mp3'
			innerAudioContext.onPlay(() => {
					console.log('开始播放')
			});	
			//
			var time = Date.parse(new Date()).toString();
			time = time.substr(0,10);
			var encryptedStr = md5(this.data.options_id + this.qian_num + time + 'wx4e358e1f2ead4c7b');
			//
			// console.log(encryptedStr)
			app.request(app.apiDomain + "index.php?c=group&a=shang",{
				id:that.data.options_id,
				type:that.qiam_type,
				money:that.qian_num,
				sign:encryptedStr,
				time:time,
			}, function(res) {
				// console.log(res.data.info);
				if(res.data.status==1){
					wx.showToast({
						title: '打赏成功',
						icon: 'none',
						duration: 1000
					});
					that.setData({
						['Coterie_detail.shang_num']: parseFloat(that.data.Coterie_detail.shang_num)+1
					}) 
				}else{
					wx.showToast({
						title: res.data.info,
						icon: 'none',
						duration: 1000
					});
				};
				that.setData({
					qian_on:''
				})
			});
		}else{
			wx.showToast({
				title: '请选择打赏金额',
				icon: 'none',
				duration: 1000
			})
		}
	},
	// 回复
	hui_fu_click:function(e){
		// var that = this;
		// console.log(e.currentTarget.dataset.name)
		if(e.currentTarget.dataset.name){
			var a='@'+e.currentTarget.dataset.name+': ';
		}else{
			var a='';
		};
		this.setData({
			focus_true:true,
			wicket_on:'on',
			text_value:a
		});
		this.tie_id = this.data.Coterie_detail.id;
		this.pid = e.currentTarget.dataset.pid ? e.currentTarget.dataset.pid:0;
		this.index = e.currentTarget.dataset.index;
		// console.log(this.tie_id,this.pid)
	},
	form_submit:function(e){
		var that = this;
		if(e.detail.value.textarea.length == 0 || e.detail.value.textarea=='限制100个字'){
			wx.showToast({
				title: '输入文字有误',
				icon: 'none',
				duration: 1000
			})
			return false
		}
		// 
		app.request(app.apiDomain + "index.php?c=group&a=addComment", {
			article_id:that.tie_id,
			content:e.detail.value.textarea,
			parent_id:that.pid,
			is_anonymous:0,
		},function(res) {
			// console.log()
			wx.showToast({
				title: res.data.info,
				icon: 'none',
				duration: 1000
			});
			that.setData({
				wicket_on:''
			})
			if(res.data.status==0){ return false}
			var d = {}
			d.username = wx.getStorageSync('nickname');
			d.content = e.detail.value.textarea;
			d.floors = that.data.comment.list.length+1;
			d.icon = wx.getStorageSync('icon');
			if(that.index>=0){
				that.data.comment.list[that.index].child_list.unshift(d);
				that.setData({
					['comment.list']:that.data.comment.list
				})
			}else{
				// console.log(1)
				if(!that.data.comment.list){
					that.data.comment.list=[];
				}
				that.data.comment.list.unshift(d);
				// console.log(that.data.comment.list)
				that.setData({
					['comment.list']:that.data.comment.list,
					['comment.total.total_num']:'1'
				})
				// console.log(that.data.comment.total.total_num)
			}
		},'POST');
	},
	wicket_off:function(){
		this.setData({
			wicket_on:'',
		});
	},
	an_click:function(e){
		let a = e.currentTarget.dataset.num;
		console.log(this.data.comment.list[a].addtime);
		if(this.data.comment.list[a].addtime=='on'){
			var c = 'false'
		}else{
			var c = 'on'
		};
		this.setData({
			['comment.list['+a+'].addtime']:c
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		app.SystemInfo(this)
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		return app.onShareAppMessage();
	},
	scroll: function() {
		if (this.suo) {
			return false
		}
		var that = this;
		this.index_num++
		console.log(this.index_num)
		app.request(app.apiDomain + "index.php?c=group&a=index", {
			page: that.index_num,
			size: '20',
			type: that.type_num,
		}, function(res) {
			if (res.data.data.artile_list == '[]' || res.data.data.artile_list == '') {
				wx.showToast({
					title: '已经到底了',
					icon: 'none',
					duration: 1000
				})
				that.suo = true;
			} else {
				that.setData({
					Coterie_list: that.data.Coterie_list.concat(res.data.data.artile_list),
				})
			}
		});
	},
	foucus:function(){
		this.setData({
			foucus_num:'type_1'
		})
	},
	blur:function(){
		this.setData({
			foucus_num:''
		})
	},
})
